#ifndef __NORM_H__
#define __NORM_H__

#include <vector>
#include <cmath>
#include "Config.h"

Real my_vec_norm(Real *x, int n, int norm_type)
{
    Real res = 0;
    switch (norm_type)
    {
    case 1:
        for (int i = 0; i < n; ++i)
        {
            res += std::abs(x[i]);
        }
        res /= n;
        break;
    case 2:
        for (int i = 0; i < n; ++i)
        {
            res += x[i] * x[i];
        }
        res /= n;
        res = std::sqrt(res);
        break;
    case -1:
        for (int i = 0; i < n; ++i)
        {
            if (std::abs(x[i]) > res)
                res = std::abs(x[i]);
        }
        break;
    default:
        break;
    }
    return res;
}

Real my_conv_rate(Real *norm, int n)
{
    Real cr = 0;
    for (int i = 1; i < n; ++i)
    {
        cr += (log(norm[i - 1]) - log(norm[i])) / log(2);
    }
    return cr /= (n - 1);
}

#endif // __NORM_H__